<template>
  <div>
    <el-card>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleFormRef" label-width="100px">
        <el-form-item label="房间名" prop="bName">
          <el-input v-model="ruleForm.bName"></el-input>
        </el-form-item>
        <el-form-item label="报修人邮箱">
          <el-input v-model="ruleForm.uEmail" disabled></el-input>
        </el-form-item>
        <el-form-item label="报修信息" prop="repair">
          <el-input v-model="ruleForm.repair"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
    ruleForm: {
        bName: '',
        uEmail: window.sessionStorage.getItem('loginUserUmail'),
        repair: ''
      },
      rules: {
        bName: {required: true, message: '请输入房间名称', trigger: 'blur'},
        repair: {required: true, message: '请输入报修信息', trigger: 'blur'},
      }
    }
  },
  methods: {
      resetForm() {
        this.$refs.ruleFormRef.resetFields();
      },
      submitForm() {
        this.$refs.ruleFormRef.validate((valid) => {
          if(!valid) return;
          this.doSubmit();
        })
      },
      async doSubmit(){
        let url = 'api/repair/addRepair';
        let params = this.$qs.stringify({
          房间名: this.ruleForm.bName,
          报修人邮箱: this.ruleForm.uEmail,
          报修信息: this.ruleForm.repair
        });
        const {data: res} = await this.$axios.post(url, params);
        if(res.code !== 200) {
          this.resetForm();
          return this.$message.error(res.data);
        }
        this.$message.success(res.data);
      }
  }
}
</script>

<style lang="less" scoped>

</style>